package logic

import (
	"context"
	"errors"
	"houseRpc/models"

	"houseRpc/house"
	"houseRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *house.UpdateUserRequest) (*house.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var u models.User
	var ur models.RealName
	var h models.House
	u.ID = uint(in.UserId)
	ur.UserId = in.UserId
	h.UserId = in.UserId

	if err = h.GetHouseByUserId(); err != nil {
		logx.Error("通过用户Id获取房屋信息失败")
		return nil, errors.New("通过用户Id获取房屋信息失败")
	}
	if h.ID != 0 {
		logx.Error("当用户有当前发布的房源，不允许修改个人信息")
		return nil, errors.New("当用户有当前发布的房源，不允许修改个人信息")
	}

	u.Mobile = in.Mobile
	ur.RealName = in.RealName
	ur.IdCard = in.IdCard

	return &house.UpdateUserResponse{
		UserId: int64(u.ID),
	}, nil
}
